/**
 * Created by saber on 15/8/13.
 */

var gulp = require('gulp');
var connect = require('gulp-connect');
var jshint = require('gulp-jshint');
var watch = require('gulp-watch');
var sourcemaps = require('gulp-sourcemaps');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var open = require('gulp-open');
var plumber = require('gulp-plumber');

var del = require('del');
var uglify = require('gulp-uglify');
var usemin = require('gulp-usemin');
var minifyCss = require('gulp-minify-css');
var cache = require('gulp-cache');
var htmlmin = require('gulp-htmlmin');
var templateCache = require('gulp-angular-templatecache');


gulp.task('lint', function () {
    return gulp.src(['src/scripts/**/*.js'])
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(jshint.reporter('fail'));
});

gulp.task('watch-sass', function () {
    return gulp.src('src/styles/*.scss')
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sass({errLogToConsole: true}))
        .pipe(autoprefixer({browsers: ['last 2 versions', 'last 4 Android versions']}))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('.tmp/styles'))
        .pipe(connect.reload());
});

gulp.task('watch', function () {
    return gulp.watch('src/styles/**/*.scss', ['watch-sass']);
});

gulp.task('connect', function () {
    connect.server({
        port: 8888,
        livereload: true,
        root: ['src/', '.tmp/']
    });
});

gulp.task('open', function () {
    return gulp.src('src/index.html')
        .pipe(open({uri: 'http://localhost:8888/#/main'})); // just for now
});

// default task
gulp.task('default',
    ['lint', 'connect', 'watch-sass', 'watch', 'open']);

gulp.task('clean', function (cb) {
    del(['build/**'], cb);
});

gulp.task('usemin', ['clean'], function () {
    return gulp.src('src/index.html')
        .pipe(usemin({
            js: [sourcemaps.init(), uglify(), sourcemaps.write('.')],
            js1: [sourcemaps.init(), uglify(), sourcemaps.write('.')],
            css: [sourcemaps.init(), autoprefixer({browsers: ['last 2 version']}), minifyCss({
                comments: true,
                spare: true
            }), sourcemaps.write('.')]
        }))
        .pipe(gulp.dest('build'));
});

gulp.task('minify-scss', ['clean'], function () {
    return gulp.src(['src/styles/*.scss'], {base: 'src/styles'})
        .pipe(sourcemaps.init())
        .pipe(sass({errLogToConsole: true}))
        .pipe(autoprefixer({browsers: ['last 2 versions', 'last 4 Android versions']}))
        .pipe(minifyCss({comments: true, spare: true}))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('build/styles'));
});


gulp.task('copy-assets', ['clean'], function () {
    return gulp.src(['src/assets/**'], {base: 'src/assets'})
        .pipe(gulp.dest('build/assets/'));
});

gulp.task('copy-html-files', ['clean'], function () {
    return gulp.src(['src/templates/**/*.html', 'src/templates/*.html', '!src/index.html'], {base: 'src'})
        .pipe(gulp.dest('build/'));
});

gulp.task('copy-root-html-files', ['clean'], function () {
    return gulp.src(['src/*.html', '!src/index.html'], {base: 'src'})
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest('build/'));
});


gulp.task('cache-templates', ['clean'], function () {
    return gulp.src(['src/templates/**/*.html'])
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(templateCache('templates.min.js', {
            module: 'wc',
            root: 'templates/'
        }))
        .pipe(gulp.dest('build/scripts'));
});

gulp.task('connect-build', function () {
    connect.server({
        root: ['build/'],
        port: 9000
    });
});

gulp.task('open-build', function () {
    return gulp.src('build/index.html')
        .pipe(open({uri: 'http://localhost:9000/#/main'})); // just for now
});


// build task
gulp.task('build',
    ['clean', 'usemin', 'minify-scss', 'copy-root-html-files', 'cache-templates', 'copy-assets', 'connect-build', 'open-build']
);